﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using QuanLyNhaThuoc.Models;
using QuanLyNhaThuoc.HelperClass;
using System.Text;
using System.Configuration;

namespace QuanLyNhaThuoc.Controllers
{
    public class AdminController : Controller
    {
        PMOEntities PMOEn = new PMOEntities();

        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            #region Check session login
            if (HttpContext.Session["USERID"] == null)
            {
                Response.Redirect("~/Account/LogIn");
            }
            #endregion

            #region Check function permission
            string controller = RouteData.Values["controller"].ToString();
            string action = RouteData.Values["action"].ToString();

            var query = PMOEn.CheckFunctionPermission(Session["USERID"].ToString(), controller, action).ToList();
            if (query.Count != 0)
            {
                ViewBag.Title = query[0].Title;
            }
            else
            {
                Response.Redirect("~/Error/Denied");

            }
            #endregion

            base.OnActionExecuting(filterContext);
        }

        //
        // GET: /Admin/

        public ActionResult Permission()
        {
            AdminModel adMo = new Models.AdminModel();
            try
            {
                
                    StringBuilder sb = new StringBuilder();
                    PMOEntities PMOEn = new PMOEntities();
                    var query = PMOEn.MenuLoad("", "0");
                    foreach (var item in query.ToList())
                    {
                        sb.Append("<li>");
                        sb.Append("<input type=\"checkbox\" id='MID" + item.MenuID + "'/>");                       
                        sb.Append("<label>");
                        sb.Append(item.MenuName);
                        sb.Append("</label>");

                        //load function of menu
                        var functionP = PMOEn.LoadFunctionOfMenu(item.MenuID.ToString()).ToList();
                        foreach (var functionChild in functionP)
                        {
                            sb.Append("<ul>");
                            sb.Append("<li>");
                            sb.Append("<input type=\"checkbox\" id='FID" + functionChild.FID + "'/>");
                            sb.Append("<label style='color:#00a65a;'>");
                            sb.Append(functionChild.Name);
                            sb.Append("</label>");
                            sb.Append("</li>");
                            sb.Append("</ul>");
                        }

                        //load child menu
                        var child = PMOEn.MenuLoad("", item.MenuID.ToString()).ToList();
                        foreach (var itemChild in child)
                        {
                            sb.Append("<ul>");
                            sb.Append("<li>");
                            sb.Append("<input type=\"checkbox\" id='MID" + itemChild.MenuID + "'/>");                           
                            sb.Append("<label>");
                            sb.Append(itemChild.MenuName);
                            sb.Append("</label>");
                            //load function of menu
                            var function = PMOEn.LoadFunctionOfMenu(itemChild.MenuID.ToString()).ToList();
                            foreach (var functionChild in function)
                            {
                                sb.Append("<ul>");
                                sb.Append("<li>");
                                sb.Append("<input type=\"checkbox\" id='FID" + functionChild.FID + "'/>");
                                sb.Append("<label style='color:#00a65a;'>");
                                sb.Append(functionChild.Name);
                                sb.Append("</label>");                                
                                sb.Append("</li>");
                                sb.Append("</ul>");
                            }

                            sb.Append("</li>");
                            sb.Append("</ul>");
                        }
                        sb.Append("</li>");
                    }

                    adMo.MenuString = sb.ToString();                    
               
                //load corporation                
                var corp=PMOEn.LoadStore("").ToList();
                List<SelectListItem> list = new List<SelectListItem>();
                SelectListItem all = new SelectListItem();
                all.Text = ConfigurationManager.AppSettings["All"];
                all.Value = "";
                list.Add(all);

                foreach (var item in corp)
                {
                    SelectListItem sli=new SelectListItem();
                    sli.Text=item.CorpName;
                    sli.Value = item.CorpID.ToString();
                    list.Add(sli);
                }

                adMo.CorpList = list;

                //load Group
                adMo.GroupList = PMOEn.LoadGroup("").ToList();
                
            }
            catch (Exception ex)
            {
                Log.LogError(ex);
                return RedirectToAction("Index", "Error");                
            }

            return View(adMo);
        }

        public ActionResult ChangeCorp(string id)
        {
            AdminModel adMo = new Models.AdminModel();
            //load Group
            adMo.GroupList = PMOEn.LoadGroup(id).ToList();
            return View(adMo);
        }

        public ActionResult LoadMenuOfGroup(string id)
        {
            AdminModel adMo = new Models.AdminModel();

            StringBuilder sb = new StringBuilder();
            PMOEntities PMOEn = new PMOEntities();           

            var query = PMOEn.MenuLoad("", "0");
            foreach (var item in query.ToList())
            {
                sb.Append("<li>");

                if (PMOEn.AC_MenuRight.Where(m => m.MenuID == item.MenuID).Where(m => m.GroupID == new Guid(id)).Count() != 0)
                {
                    sb.Append("<input type=\"checkbox\" checked='checked' id='MID"+item.MenuID+"' />");
                }
                else
                {
                    sb.Append("<input type=\"checkbox\" id='MID" + item.MenuID + "'/>");
                }

                sb.Append("<label>");
                sb.Append(item.MenuName);
                sb.Append("</label>");

                //load function of menu
                var functionP = PMOEn.LoadFunctionOfMenu(item.MenuID.ToString()).ToList();
                foreach (var functionChild in functionP)
                {
                    sb.Append("<ul>");
                    sb.Append("<li>");
                    if (PMOEn.AC_FunctionRight.Where(m => m.FID == functionChild.FID).Where(m => m.GroupID == new Guid(id)).Count() != 0)
                    {
                        sb.Append("<input type=\"checkbox\" checked='checked' id='FID" + functionChild.FID + "'/>");
                    }
                    else
                    {
                        sb.Append("<input type=\"checkbox\" id='FID" + functionChild.FID + "'/>");
                    }
                    sb.Append("<label style='color:#00a65a;'>");
                    sb.Append(functionChild.Name);
                    sb.Append("</label>");
                    sb.Append("</li>");
                    sb.Append("</ul>");
                }

                //load child menu
                var child = PMOEn.MenuLoad("", item.MenuID.ToString()).ToList();
                foreach (var itemChild in child)
                {
                    sb.Append("<ul>");
                    sb.Append("<li>");
                    if (PMOEn.AC_MenuRight.Where(m => m.MenuID == itemChild.MenuID).Where(m => m.GroupID == new Guid(id)).Count() != 0)
                    {
                        sb.Append("<input type=\"checkbox\" checked='checked' id='MID" + itemChild.MenuID + "'/>");
                    }
                    else
                    {
                        sb.Append("<input type=\"checkbox\" id='MID" + itemChild.MenuID + "'/>");
                    }
                    sb.Append("<label>");
                    sb.Append(itemChild.MenuName);
                    sb.Append("</label>");
                    //load function of menu
                    var function = PMOEn.LoadFunctionOfMenu(itemChild.MenuID.ToString()).ToList();
                    foreach (var functionChild in function)
                    {
                        sb.Append("<ul>");
                        sb.Append("<li>");
                        if (PMOEn.AC_FunctionRight.Where(m => m.FID == functionChild.FID).Where(m => m.GroupID == new Guid(id)).Count() != 0)
                        {
                            sb.Append("<input type=\"checkbox\" checked='checked' id='FID" + functionChild.FID + "'/>");
                        }
                        else
                        {
                            sb.Append("<input type=\"checkbox\" id='FID" + functionChild.FID + "'/>");
                        }
                        sb.Append("<label style='color:#00a65a;'>");
                        sb.Append(functionChild.Name);
                        sb.Append("</label>");
                        sb.Append("</li>");
                        sb.Append("</ul>");
                    }

                    sb.Append("</li>");
                    sb.Append("</ul>");
                }
                sb.Append("</li>");
            }

            adMo.MenuString = sb.ToString(); 
            return View(adMo);
        }

        public ActionResult Save(string fmid,string groupid)
        {
            PMOEntities PMOEn = new PMOEntities();
            int result= PMOEn.SetRight(groupid, fmid.Substring(1,fmid.Length-1)).ToList()[0].Result;
            if (result == 0)
            {
                return Content("Thiết lập quyền thành công");
            }
            else
            {
                return Content("Thiết lập quyền lỗi"); 
            }
        }
    }
}
